package com.metadata_system.api_server.system_server;

import com.alibaba.fastjson.JSON;
import com.metadata_system.api.annotations.CheckTokenUser;
import com.metadata_system.common.db.entity.MetadataNode;
import com.metadata_system.common.dto.ApiResult;
import com.metadata_system.common.http.request_dto.LoginUserRequest;
import com.metadata_system.system.request.MetadataNodeListParams;
import com.metadata_system.system.response.NodePropertiesItem;
import com.metadata_system.system.service.MetadataNodeService;
import com.metadata_system.system.service.MetadataService;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.transaction.interceptor.TransactionAspectSupport;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/api/system-server/v001/MetadataNode")
public class MetadataNodeController {

    @Autowired
    private MetadataNodeService metadataNodeService;

    @Autowired
    private MetadataService metadataService;

    @Autowired
    LoginUserRequest loginUserRequest;

    @RequestMapping("getList")
    @CheckTokenUser
    public ApiResult getList(
            @RequestParam(value = "metadataViewId", required = false) Integer metadataViewId,
            @RequestParam(value = "metadataId", required = false) Integer metadataId,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "page", required = false) Integer page,
            @RequestParam(value = "pageSize", required = false) Integer pageSize,
            @RequestParam(value = "sortProp", required = false) String sortProp,
            @RequestParam(value = "sortOrder", required = false) String sortOrder
    ) {
        return metadataNodeService.getList(MetadataNodeListParams.builder()
                .name(name)
                .metadataViewId(metadataViewId)
                .metadataId(metadataId)
                .page(page)
                .pageSize(pageSize)
                .sortProp(sortProp)
                .sortOrder(sortOrder)
                .build());
    }

    @RequestMapping("create")
    @CheckTokenUser
    public ApiResult create(
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "fromNodeId", required = false) String fromNodeId,
            @RequestParam(value = "position", required = false) String position,
            @RequestParam(value = "uniqueKey", required = false) String uniqueKey,
            @RequestParam(value = "properties", required = false, defaultValue = "[]") String properties,
            @RequestParam(value = "metadataViewId", required = false) Integer metadataViewId,
            @RequestParam(value = "metadataId", required = false) Integer metadataId
    ) {
        if (fromNodeId == null) {
            fromNodeId = "";
        }
        return metadataNodeService.create(MetadataNode.builder()
                .name(name)
                .position(position)
                .uniqueKey(uniqueKey)
                .properties(properties)
                .metadataViewId(metadataViewId)
                .metadataId(metadataId)
                .build(),fromNodeId,loginUserRequest.getTokenUser().decodeID());
    }

    @RequestMapping("update")
    @CheckTokenUser
    @Transactional
    public ApiResult update(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "name", required = false) String name,
            @RequestParam(value = "position", required = false) String position,
            @RequestParam(value = "uniqueKey", required = false) String uniqueKey,
            @RequestParam(value = "properties", required = false) String properties
    ) {
        ApiResult apiResult = metadataNodeService.update(MetadataNode.builder()
                .id(id)
                .name(name)
                .position(position)
                .uniqueKey(uniqueKey)
                .properties(properties)
                .build(),loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }


    @RequestMapping("updateMore")
    @CheckTokenUser
    @Transactional
    public ApiResult updateMore(
            @RequestParam(value = "changeNodes", required = false) String changeNodes,
            @RequestParam(value = "metadataViewId", required = false) Integer metadataViewId
    ) {
        List<MetadataNode> metadataNodes = JSON.parseArray(changeNodes, MetadataNode.class);

        for (MetadataNode metadataNode : metadataNodes) {
            ApiResult tmpApiresult = metadataNodeService.update(MetadataNode.builder()
                    .id(metadataNode.getId())
                    .name(metadataNode.getName())
                    .position(metadataNode.getPosition())
                    .properties(metadataNode.getProperties())
                    .build(),loginUserRequest.getTokenUser().decodeID());
            if (tmpApiresult.getCode() != 0) {
                TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
                return tmpApiresult;
            }
        }
        return metadataService.getDetailByVewId(metadataViewId);
    }


    @RequestMapping("delete")
    @CheckTokenUser
    @Transactional
    public ApiResult delete(
            @RequestParam(value = "id", required = false) Integer id,
            @RequestParam(value = "metadataViewId", required = false) Integer metadataViewId
    ) {
        ApiResult apiResult = metadataNodeService.delete(id,metadataViewId,loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }
        return apiResult;
    }


    @RequestMapping("deleteMore")
    @CheckTokenUser
    @Transactional
    public ApiResult deleteMore(
            @RequestParam(value = "ids", required = false) String ids,
            @RequestParam(value = "metadataViewId", required = false) Integer metadataViewId
    ) {
        List<Integer> idList = new ArrayList<>();
        try {
            idList = JSON.parseArray(ids, Integer.class);
            for (Integer id : idList) {
                if (id <= 0) {
                    return ApiResult.error("删除节点-参数错误 1");
                }
            }
        } catch (Exception e) {
            return ApiResult.error("删除节点-参数错误 2");
        }

        ApiResult apiResult = metadataNodeService.deleteAll(idList, metadataViewId,loginUserRequest.getTokenUser().decodeID());
        if (apiResult.getCode() != 0) {
            TransactionAspectSupport.currentTransactionStatus().setRollbackOnly();
        }

        return apiResult;
    }


}
